var tableColShowMixin = function (typeName) {
  var columnName = '';
  var currentColumnName = '';
  switch (typeName) {
    case 'purchaseOrder':
      columnName = ["purchaseOrder-order", "purchaseOrder-warehouse"];
      currentColumnName = "purchaseOrder-order";
      break;

    default:
      columnName = typeName;
      currentColumnName = 'columns';
      break;
  }

  var localSaveList = ['purchaseinWarehouseMaterial'];

  return {
    watch: {
      tableHideColumnKey: function () {
        var self = this;
        this.$nextTick(function () {
          if (self.$refs[typeName] !== undefined) {
            self.$refs[typeName].doLayout();
          }
        });
      }
    },
    data: function () {
      return {
        tableColumnKeyLoading: false,
        tableName: typeName,
        columnName: columnName,
        currentColumnName: currentColumnName,
        tableHideColumnKey: null,
        remoteSave: localSaveList.indexOf(typeName) === -1,
        // tableId: SysUtil.getId()
      };
    },
    created: function () {
      this.setTableHideColumnKey();
    },
    methods: {
      setTableHideColumnKey: function () {
        this.tableColumnKeyLoading = true;
        var self = this;
        this.getTableHideColumnKey().then(function (tableHideColumnKey) {
          self.tableColumnKeyLoading = false;
          if (tableHideColumnKey === null) {
            tableHideColumnKey = {};
          }
          self.tableHideColumnKey = tableHideColumnKey;
        }).catch(function () {
          self.tableColumnKeyLoading = false;
        });
      },

      getTableHideColumnKey: function () {
        if (!this.remoteSave) {
          var tableHideColumnKey;
          var config = localStorage.getItem('tableColConfig');
          if (SysUtil.isEmpty(config)) {
            tableHideColumnKey = null;
          } else {
            config = JSON.parse(config);
            tableHideColumnKey = config[this.tableName];
            if (SysUtil.isEmpty(tableHideColumnKey)) {
              tableHideColumnKey = null;
            }
          }
          return Promise.resolve(tableHideColumnKey);
        }

        return ajaxRequest('tableCoucmnlConfig/getConfigByName', {
          tableName: this.tableName
        }).then(function (config) {
          var tableHideColumnKey;
          if (config === null) {
            tableHideColumnKey = {};
          } else {
            var hideColumn = config.hideColumn;
            if (SysUtil.isEmpty(hideColumn)) {
              tableHideColumnKey = {};
            } else {
              tableHideColumnKey = JSON.parse(hideColumn);
            }
          }
          return tableHideColumnKey;
        });
      },

      tableNameChange: function (tableName) {
        this.tableName = tableName;
      },

      getTableHideColumns: function () {
        var tableHideColumnKey = this.tableHideColumnKey;
        if (tableHideColumnKey === null) {
          return [];
        }

        var currentColumnName = this.currentColumnName;
        var columns = tableHideColumnKey[currentColumnName];
        if (columns === undefined) {
          columns = [];
        }
        return columns;
      }
    },
    components: {
      TableColShowSelect: TableColShowSelect
    }
  };
}
